/*
 * Copyright：fxsen <br/>
 * Date: 2021/9/23 10:03 <br/>
 * Author: <a href="mailto:fxsen@foxmail.com">fxsen</a>
 */
package com.fxsen.commons.sysmanager.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxsen.commons.core.base.common.service.impl.CommonServiceImpl;
import com.fxsen.commons.core.constant.Constants;
import com.fxsen.commons.sysmanager.core.entity.Menu;
import com.fxsen.commons.sysmanager.core.mapper.MenuMapper;
import com.fxsen.commons.sysmanager.core.service.IMenuService;
import com.fxsen.commons.sysmanager.core.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @describe: 系统菜单接口实现 <br/>
 * @date: 2021/9/23 10:03 <br/>
 * @author: <a href="mailto:fxsen@foxmail.com">fxsen</a>
 */
@Service
public class MenuServiceImpl extends CommonServiceImpl<MenuMapper,Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private IUserService userService;
    @Override
    public List<Menu> selectList() {
        QueryWrapper<Menu> wrapper=new QueryWrapper<>();
        wrapper.eq("delete_logic", Constants.NO_DELETE);
        wrapper.eq("status", Constants.STATUS_YES);
        wrapper.orderByAsc("order_index");
        return menuMapper.selectList(wrapper);
    }

    @Override
    public Map<String, Object> initMenu(String userId) {
        Map<String, Object> map = new HashMap<>(16);
        Map<String, Object> home = new HashMap<>(16);
        List<Map<String, Object>> homeList = new ArrayList<>();
        List<Map<String, Object>> logoList = new ArrayList<>();
        home.put("href","/welcome");
        home.put("icon","fa fa-home");
        home.put("title","首页");
        Map<String, Object> logo = new HashMap<>(16);
        logo.put("href","/");
        logo.put("title","小娜智能家居平台");
        /*logo.put("image","layui/images/favicon.ico");*/
        homeList.add(home);logoList.add(logo);
        map.put("homeInfo",home);
        map.put("logoInfo",logo);
        List<Menu> menuList = menuMapper.findByUserId(userId);
        /*List<MenuDto> menuInfo = new ArrayList<>();
        for (Menu e : menuList) {
            MenuDto menuDto = new MenuDto();
            menuDto.setId(e.getId());
            menuDto.setPid(e.getPid());
            menuDto.setHref(e.getHref());
            menuDto.setTitle(e.getTitle());
            menuDto.setIcon(e.getIcon());
            menuDto.setTarget(e.getTarget());
            menuInfo.add(menuDto);
        }*/
        //map.put("menuInfo", toTree(menuInfo));
        return map;
    }

    @Override
    public List<Menu> findByPid(String pId) {
        return menuMapper.findByPid(pId);
    }

    @Override
    public List<Menu> findByRoleId(String roleId) {
        return menuMapper.findByRoleId(roleId);
    }

    @Override
    public List<Menu> findByUserId(String userId) {
        return menuMapper.findByUserId(userId);
    }

    /*private static List<MenuDto> toTree(List<MenuDto> treeList) {
        List<MenuDto> retList = new ArrayList<MenuDto>();
        for (MenuDto parent : treeList) {
            if (parent.getPid().equals(MenuDto.PARENT_ID)) {
                retList.add(findChildren(parent, treeList));
            }
        }
        return retList;
    }
    private static MenuDto findChildren(MenuDto parent, List<MenuDto> treeList) {
        for (MenuDto child : treeList) {
            if (parent.getId().equals(child.getPid())) {
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<>());
                }
                parent.getChild().add(findChildren(child, treeList));
            }
        }
        return parent;
    }*/
}
